package com.linian.pushforcampus.service.Impl;


import com.alibaba.fastjson.JSONObject;
import com.linian.pushforcampus.domain.PushConfigure;
import com.linian.pushforcampus.service.ExternalService;
import com.linian.pushforcampus.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ExternalServiceImpl implements ExternalService {

    @Autowired
    private PushConfigure pushConfigure;

//    @Autowired


    @Override
    public String getTokenAndStudentId(String name, String password) {
        // 构造参数数据
        String data = "?userName=" + name + "&pwd=" + password;
        // 发送HTTP GET请求获取JSON数据
        String httpJsonByUrl = HttpUtil.getHttpJsonByUrl(pushConfigure.getCAMPUSAPYTOKENURL() + data);
        // 用fastjson解析JSON数据
        JSONObject jsonObject = JSONObject.parseObject(httpJsonByUrl);
        // 判断返回的JSON中的state是否为200
        if (jsonObject.getString("state").equals("200")) {
            // 如果state为200，则返回数据
            if(jsonObject.getString("x_id_token")==null) return null;
            return jsonObject.getString("x_id_token");
        } else {
            // 如果state不为200，则打印日志，表示获取token失败
            log.info("获取token失败");
            // 返回null
            return null;
        }
    }

}
